package com.epoint.action.clubInfo;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoint.action.activityinfo.ActivityListAction;
import com.epoint.bizlogic.clubInfo.ClubInfoService;
import com.epoint.bizlogic.clubInfo.domain.ClubInfo;
import com.epoint.bizlogic.clubInfo.domain.ClubInfoVo;
import com.epoint.utils.RequestUtils;

/**
 * Servlet implementation class ClubListAction
 */
@WebServlet("/clublistaction")
public class ClubListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	ClubInfoService clubInfoService = new ClubInfoService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClubListAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String requestMethod = request.getParameter("method");
        String json = RequestUtils.reflectInvokMethod(requestMethod, request, ClubListAction.class);
        response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	/**
     * 分页模糊查询信息
     * @param pageIndex 页号
     * @param pageSize 页数
     * @param clubName 俱乐部名称
     * @param clubType 类型
     * @return
     */
    public String queryClubInfo(HttpServletRequest request) {
         String pageIndex = request.getParameter("pageIndex");
         String pageSize = request.getParameter("pageSize");
         String clubName = request.getParameter("clubName");
         String clubType = request.getParameter("clubType");
         
         List<ClubInfoVo> list = clubInfoService.queryClubInfo(Integer.valueOf(pageIndex), Integer.valueOf(pageSize), clubName, clubType);
         int total = clubInfoService.queryClubInfo(null, null, clubName, clubType).size();
         Map<String, Object> reslut = new HashMap<>(2);
         reslut.put("total", total);
         reslut.put("data", list);
         return JSONObject.toJSONStringWithDateFormat(reslut, "yyyy-MM-dd");
     }
    
    /**
     * 根据id查询俱乐部信息
     * @param clubId id
     * @return 结果
     */
    public String queryClubInfoById(HttpServletRequest request) {
        String clubId = request.getParameter("clubId");
        ClubInfo clubInfo = clubInfoService.queryClubInfoById(clubId);
        return JSON.toJSONStringWithDateFormat(clubInfo, "yyyy-MM-dd");
    }
    
    /**
     * 获取全部俱乐部信息
     * @param request 请求
     * @return
     */
    public String queryAllClubInfo(HttpServletRequest request) {
        List<ClubInfoVo> list = clubInfoService.queryAllClubInfo();
        return JSONObject.toJSONStringWithDateFormat(list, "yyyy-MM-dd");
    }

}
